<template>
  <el-dialog title="详细信息" v-model="Visible" width="200" :before-close="closeDrawer">
    <el-form ref="formRef" :model="formData" class="form-inline" label-width="100px" :rules="formRules">
      <el-form-item label="平台类型" prop="platformType">
        <el-select v-model="formData.platformType">
          <el-option v-for="item in getEnumBySn('daikuanTypeEnum')" :label="item.label"
                     :key="item.label"
                     :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="平台" prop="platform">
        <el-input v-model="formData.platform" class="input_width"></el-input>
      </el-form-item>
      <el-form-item label="金额">
        <el-input-number v-model="formData.trusteeshipAmount"></el-input-number>
      </el-form-item>
      <el-form-item label="逾期时间" prop="overdueDateTime">
        <el-date-picker
          v-model="formData.overdueDateTime"
          type="date"
          placeholder="选择日期"
          value-format="YYYY-MM-DD"
          clearable
        />
      </el-form-item>
      <div v-show="formData.latestMaintenanceId">
        <el-form-item label="优先级" prop="type">
          <el-select
            v-model="formData.type"
            placeholder="请选择优先级"
            clearable
          >
            <el-option
              v-for="item in getEnumBySn('priority')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="处理人" prop="handleUser">
          <el-select
            v-model="formData.handleUser"
            placeholder="请选择处理人"
            clearable
          >
            <el-option
              v-for="item in groupList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="处理状态">
          <el-select v-model="formData.handleStatus" clearable>
            <el-option v-for="item in getEnumBySn('maintenanceStatus')" :key="item.value" :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理进度">
          <el-select v-model="formData.handleProgress" clearable :disabled="formData.handleStatus!='已完成'">
            <el-option v-for="item in getEnumBySn('trusteeshipStatus')" :key="item.value" :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="formData.handleMemo"></el-input>
        </el-form-item>
      </div>
    </el-form>
    <div style="height: 100px;">
      <div style="float: right;">
        <el-button @click="closeDrawer">取消</el-button>
        <el-button type="primary" @click="saveItem">保存</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script setup>
import {ref, defineEmits, watch, onMounted} from 'vue';
import {getEnumBySn} from "@/api/enums";
import request from '@/config/axios'
import {groupList} from "@/api/enums/index";

const props = defineProps({
  orderInfo: {
    type: Object,
    required: true,
  }
});
const Visible = ref(false);
const formRef = ref()
const formData = ref({});
const formRules = ref({})

const addRules = {
  platformType: [
    {required: true, message: "请选择平台类型", trigger: 'change'}
  ],
  platform: [
    {required: true, message: "请输入平台信息", trigger: 'change'}
  ],
  hasInfo: [
    {required: true, message: "请选择资料", trigger: 'change'}
  ]
}

const whRules = {
  platformType: [
    {required: true, message: "请选择平台类型", trigger: 'change'}
  ],
  platform: [
    {required: true, message: "请输入平台信息", trigger: 'change'}
  ],
  hasInfo: [
    {required: true, message: "请选择资料", trigger: 'change'}
  ],
  type: [
    {required: true, message: "请选择优先级", trigger: 'change'}
  ]
}

const emit = defineEmits(['close', 'save']);

function closeDrawer() {
  Visible.value = false
  emit('close');
}


async function saveItem() {
  console.log("----当前的值为-----",formData.value)
  await formRef.value?.validate().then((valid) => {
    if (!valid) {
      return
    }
  })
  try {
    let currentUrl = '/trusteeshipDetail/save'
    console.log("当前的值为",formData.value)
    if (formData.value?.id){
      currentUrl = '/trusteeshipDetail/updateById'
    }
    await request.post({
      url: currentUrl,
      data: {
        ...formData.value,
        orderId: props.orderInfo.orderId
      }
    })
    ElMessage.success("新增成功!")
    Visible.value = false
    emit('close');
  } catch (error) {
    throw error;
  } finally {
  }
}

const handleStatusChange = function(){
  formData.value.statusChange = "1"
}

defineExpose({
  add: ()=>{
    Visible.value = true
    formData.value = {}
    formRef.value?.resetFields()
    formRules.value = addRules
  },
  edit: (rowData)=>{
    Visible.value = true
    formData.value = {
      "id": rowData.id,
      "latestMaintenanceId": rowData.latestMaintenanceId,
      "platformType": rowData.platformType,
      "platform": rowData.platform,
      "trusteeshipAmount": rowData.trusteeshipAmount,
      "handleStatus": rowData.handleStatus,
      "handleProgress": rowData.handleProgress,
      "handleMemo": rowData.handleMemo,
      "type": rowData.type,
      "handleUser": rowData.handleUser,
      "overdueDateTime": rowData.overdueDateTime
    }
    if (rowData.latestMaintenanceId){
      formRules.value = whRules
    }else {
      formRules.value = addRules
    }
  }

})
</script>

<style scoped></style>
